import { PrismaClient } from '@prisma/client'
import { hashPassword } from '../lib/auth/password'

const prisma = new PrismaClient()

async function main() {
  console.log('开始初始化数据库...')
  
  // 创建测试用户
  const hashedPassword = await hashPassword('password123')
  
  const testUser = await prisma.user.upsert({
    where: { email: 'test@example.com' },
    update: {},
    create: {
      email: 'test@example.com',
      name: '测试用户',
      password: hashedPassword,
    },
  })
  
  console.log('创建测试用户成功:', testUser.id)
  
  // 创建管理员用户
  const adminUser = await prisma.user.upsert({
    where: { email: 'admin@example.com' },
    update: {},
    create: {
      email: 'admin@example.com',
      name: '管理员',
      password: await hashPassword('admin123'),
    },
  })
  
  console.log('创建管理员用户成功:', adminUser.id)
  
  // 为测试用户创建一些示例配方
  const testRecipe1 = await prisma.recipe.create({
    data: {
      nameEn: 'Mojito',
      nameZh: '莫吉托',
      category: '经典鸡尾酒',
      ingredients: [
        { name: '白朗姆酒', amount: '60ml', color: '#f5f5dc' },
        { name: '青柠汁', amount: '30ml', color: '#c1ff72' },
        { name: '薄荷叶', amount: '8-10片', color: '#3eb489' },
        { name: '糖浆', amount: '15ml', color: '#f0f8ff' },
        { name: '苏打水', amount: '适量', color: '#e0ffff' }
      ],
      method: [
        '在杯中放入薄荷叶和糖浆，轻轻捣碎薄荷叶释放香气',
        '加入青柠汁和白朗姆酒',
        '加入冰块，倒入苏打水',
        '轻轻搅拌均匀',
        '用薄荷叶和青柠片装饰'
      ],
      userId: testUser.id
    }
  })
  
  console.log('创建示例配方成功:', testRecipe1.id)
  
  const testRecipe2 = await prisma.recipe.create({
    data: {
      nameEn: 'Margarita',
      nameZh: '玛格丽特',
      category: '经典鸡尾酒',
      ingredients: [
        { name: '龙舌兰酒', amount: '50ml', color: '#f5f5dc' },
        { name: '君度橙酒', amount: '20ml', color: '#ffa500' },
        { name: '青柠汁', amount: '25ml', color: '#c1ff72' },
        { name: '盐', amount: '适量', color: '#ffffff' }
      ],
      method: [
        '在杯口抹上青柠汁，然后蘸上盐',
        '在调酒壶中加入冰块、龙舌兰酒、君度橙酒和青柠汁',
        '摇匀后倒入杯中',
        '用青柠片装饰'
      ],
      userId: testUser.id
    }
  })
  
  console.log('创建示例配方成功:', testRecipe2.id)
  
  console.log('数据库初始化完成!')
}

main()
  .catch((e) => {
    console.error('初始化数据库失败:', e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  }) 